﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Models;
namespace DAL
{
   public class RegisteredusersDAL
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        public static string conn = ConfigurationSettings.AppSettings["con"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        //注册
        public static int Register(string jh,string txtname, string phone, int ryid,string rfkh,string password ,DateTime khrq,string xb) {
	    SqlParameter[] sp = new SqlParameter[]
	    {
            new SqlParameter("@jh",jh),
        new SqlParameter("@ryid",ryid),
	    new SqlParameter("@rfkh",rfkh),
	    new SqlParameter("@password",password),
	    new SqlParameter("@khrq",khrq),
	    new SqlParameter("@xb",xb),
	    new SqlParameter("@txtname",txtname),
	    new SqlParameter("@phone",phone)
	    };

            string sql = @"insert into dbo.ryxx(rybh,ryxm,Phoneno,ryid,rfzt,bmmc,rfkh,[password],rfye,khrq,xc1,xfxe,klb,xc2,xc3,xc4,xb,grye,btye,dh,Tel,yxq,kxfjh,equipment,yj)
                       values(@rfkh, @txtname, @phone, @ryid, 32, '其他', @rfkh, @password, 0, @khrq, 255, 100000, 'B', 225, 255, 255, @xb, 0, 0, @phone, @phone, '2099-12-31 00:00:00.000',@jh,@jh,0)";
            return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql,  sp);
	    }
	//判断是否有此卡号
	public static int JudgeUser(string rfkh) {
	    SqlParameter[] sp = new SqlParameter[] {
	    new SqlParameter("@rfkh",rfkh)
	    };
	    string sql = "select count(*) from ryxx where rfkh=@rfkh";
	    return (Int32) SqlHelper.ExecuteScalar(conn,CommandType.Text, sql,sp);
	}
        //修改心跳
        public static int userinfo(string kxfjh, string Agreement1, string Agreement2)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            SqlParameter[] sp = new SqlParameter[]
        { };
            string xfjh = kxfjh;
            int sumNum = 0;
            try
            {
                string kxfjh1 = kxfjh.Replace(",", "");
                //kxfjh1 = kxfjh1.Replace("，", "");
                int kyxfjh = 0;
                int numm = 0;
                int ks = 0;
                if (kxfjh.Length > 0)
                {
                    for (int i = 0; i < (kxfjh1.Length / 3); i++)
                    {
                        if (1 > numm)
                        {
                            numm++;
                            var jh= Convert.ToInt32(kxfjh.Substring(0, 3));
                            string sqlstr = "select updateUserInfo from jhsz where jh= "+jh+"";
                            var num = SqlHelper.ExecuteDataSet(conn, CommandType.Text, sqlstr, sp);
                            var num1 = num.Tables[0].Rows[0]["updateUserInfo"].ToString();
                            bool isContains = num1.Contains("1");//true 
                            if(isContains == false)
                            {
                                if (num1 == "")
                                {
                                    var count = "1";
                                    string sqlstr1 = "update jhsz set  updateUserInfo='"+ count + "' where jh ="+jh+"";
                                    var numa = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr1, sp);
                                }
                                else
                                {
                                    var count = num1 + "&1";
                                    string sqlstr1 = "update jhsz set  updateUserInfo='" + count + "' where jh =" + jh + "";
                                    var numa = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr1, sp);
                                }
                            }
                        }
                        else
                        {
                            ks = ks + 4;
                            kyxfjh = Convert.ToInt32(kxfjh.Substring((0 + ks), 3));
                            string sqlstr = "select updateUserInfo from jhsz where jh= " + kyxfjh + "";
                            var num = SqlHelper.ExecuteDataSet(conn, CommandType.Text, sqlstr, sp);
                            var num1 = num.Tables[0].Rows[0]["updateUserInfo"].ToString();
                            bool isContains = num1.Contains("1");//true 
                            if (isContains == false)
                            {
                                if (num1 == "")
                                {
                                    var count = "1";
                                    string sqlstr1 = "update jhsz set  updateUserInfo='" + count + "' where jh =" + kyxfjh + "";
                                    var numa = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr1, sp);
                                }
                                else
                                {
                                    var count = num1 + "&1";
                                    string sqlstr1 = "update jhsz set  updateUserInfo='" + count + "' where jh =" + kyxfjh + "";
                                    var numa = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr1, sp);
                                }
                            }
                        }
                    }
                }
                return sumNum;
            }
            catch (Exception ex)
            {
                ApplicationLog.Write("报错JudgeMachine：" + ex.Message);
                return sumNum;
            }
        }
        //判断是否有此用户
        public static int User(string ryxm)
	{
	    SqlParameter[] sp = new SqlParameter[] {
	    new SqlParameter("@ryxm",ryxm)
	    };
	    string sql = "select count(*) from ryxx where ryxm=@ryxm";
	    return (Int32) SqlHelper.ExecuteScalar(conn,CommandType.Text, sql,  sp);
	}

        public static DataSet getJh()
        {
            SqlParameter[] sp = new SqlParameter[] {
        };
            string sql = "select stuff((select ','+jh from jhsz for xml path('')),1,1,'')as xfjh from qx where username='admin' ";
            return SqlHelper.ExecuteDataSet(conn, CommandType.Text, sql, sp);
        }

        public static int RxCount()
	{
	    string sql = "select count(*) from ryxx";
	    return (Int32) SqlHelper.ExecuteScalar(conn,CommandType.Text, sql);
	}
    }
}
